<?php

namespace app\index\controller;

use app\index\model\Order;
use think\Controller;
use think\Db;
use app\util\Utils;
use think\Request;

/*
 * 每日订单金额和流水金额
 */

class NewdayscheckController extends Controller {
    /*
     * 定时更新订单状态
     *
     * 订单状态为服务中(7)和美容师服务完成(8)时，如果客户没有确认服务完成，自动更新为服务完成(9)
     */

    public function dayscheck() {
        $ip=Request::instance()->ip();
        $initdata['ip']=$ip;
        $initdata['add_time']=time();
        Db::table('da_aaaaa')->insert($initdata);
        $time = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));
        $time2 = mktime(23, 59, 59, date('m'), date('d')-1, date('Y'));
//        if($ip!='47.104.18.180'){
//            return;
//        }

        $where['status'] = array('>', 1);
        $where['ordtype'] = array('in', '1,3,4,5,6,7');
        $where['type'] = array('<>', '7');
        $ordlist = Db::table('da_order')->where($where)->where('pay_time','>=',$time)->where('pay_time','<=',$time2)->field('pay_money')->select();
        $where2['status'] = array('>', 1);
        $where2['ordtype'] = array('in', '1,3,4,5,6,7');
        $where2['type'] = 7;
        $ordlist2 = Db::table('da_order')->where($where2)->where('add_time','>=',$time)->where('add_time','<=',$time2)->field('pay_money')->select();
        $ordmoney=0;
        for($i=0;$i<count($ordlist);$i++){
            $ordmoney=  bcadd($ordlist[$i]['pay_money'], $ordmoney,2);
        }
        for($i=0;$i<count($ordlist2);$i++){
            $ordmoney=  bcadd($ordlist2[$i]['pay_money'], $ordmoney,2);
        }

        $where1['type'] = 1;
        $where1['pro_type'] = array('<>', 7);
        $flowlist = Db::table('da_moneyflow')->where($where1)->where('add_time','>=',$time)->where('add_time','<=',$time2)->field('money,kkm_money')->select();
        $money=0;
        for($i=0;$i<count($flowlist);$i++){
            $money=  bcadd($flowlist[$i]['money'], $money,2);
        }
        $kkmmoney=0;
        for($i=0;$i<count($flowlist);$i++){
            $kkmmoney=  bcadd($flowlist[$i]['kkm_money'], $kkmmoney,2);
        }
        $flowmoney=$money+$kkmmoney;
                
        if ($ordmoney == $flowmoney) {
            $status = 1; //1正常2订单金额大于流水金额3订单金额小于流水金额
            $note = "正常";
        } elseif ($ordmoney > $flowmoney) {
            $status = 2;
            $aa = bcsub($ordmoney, $flowmoney, 2);
            $note = "订单金额比流水金额大" . $aa . "元";
        } elseif ($ordmoney < $flowmoney) {
            $status = 3;
            $aa = bcsub($flowmoney, $ordmoney, 2);
            $note = "订单金额比流水金额小" . $aa . "元";
        }
        $data['ordmoney'] = $ordmoney;
        $data['flowmoney'] = $flowmoney;
        $data['note'] = $note;
        $data['status'] = $status;
        $data['datatime'] = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));
        $data['add_time'] = time();
        $relust = Db::table('da_dayscheck')->insert($data);
    }

}
